<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<center>
    <font style="font-weight: bold; font-size: 23px;">股民修改</font><br>
    <form id="form" class="layui-form">
        姓名：<input type="text" class="layui-input" name="name" id="name">
        性别：<input type="radio" name="gender" title="男" value="1" id="nan">
        <input type="radio" name="gender" title="女" value="2" id="nv"><br>
        证券公司：<input type="text" class="layui-input" name="securitie"  id="securitie">
        股东账号：<input type="text" class="layui-input" name="account" id="account">
        股东市值：<input type="text" class="layui-input" name="values" id="values">
        持仓股票：
        <div id="gupiaos">
            股票代码：<input type="text" name="code" class="layui-input"> --
            股票名称：<input type="text" name="sname" class="layui-input">
        </div>
    </form>
    <button onclick="updateInvestor()" class="layui-btn">保存修改</button>
</center>
</body>
<link rel="stylesheet" href="../views/js/layui/css/layui.css">
<script type="text/javascript" src="../views/js/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="../views/js/layui/layui.all.js"></script>
<script type="text/javascript">
    var id = "";
    $(function(){
        $.post("../investor/updInit",function(data){
            var investor = data.investor;
            id = investor.id;
            console.log(id);
            var stocks = data.stocks;
            $("#name").val(investor.name);
            if(investor.gender == '1'){
                $("#nan").prop("checked",true);
            }else{
                $("#nv").prop("checked",true);
            }
            $("#securitie").val(investor.securitie);
            $("#account").val(investor.account);
            $("#values").val(investor.values);
            for(var i=0;i<stocks.length;i++){
                $("#gupiaos").append("股票代码：<input type='text' class='layui-input' value='"+stocks[i].code+"'>" +
                    " -- 股票名称：<input type='text' class='layui-input' value='"+stocks[i].name+"'>" +
                    "<button onclick='removeStock("+stocks[i].id+")' class='layui-btn'>-</button><br>");
            }
            layui.form.render();
        },'json')
    })

    function removeStock(stockId){
        $.post("../investor/delInvestorStock",{id:id,stockId:stockId},function(res){
            if(res == "ok"){
                location.reload();
            }else{
                alert("删除已有股票失败！");
            }
        },'json')
    }

    //保存修改按钮
    function updateInvestor(){
        //序列化表单
        var info = $("form").serialize();
        //发送ajax请求
        $.post("../investor/upd?id="+id,info,function(res){
            if(res == "ok"){
                alert("修改成功！");
                location.href = "../views/investorList.html";
            }else{
                alert("修改失败！");
            }
        },'json')
    }
</script>
</html>